#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QDebug>

#include "stdio.h"
#include "math.h"
#include "malloc.h"
#include <string.h>
#include <iostream>
#include <fstream>
using namespace std;

static incDataFileParam incDataFileParam_inst;

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    connect(ui->comboBox_frameIncOrDec, SIGNAL(currentIndexChanged(QString)), this, SLOT( refreshFileConfig() ) );
    connect(ui->comboBox_lineLen, SIGNAL(currentIndexChanged(QString)), this, SLOT( refreshFileConfig() ) );
    connect(ui->comboBox_frameLen, SIGNAL(currentIndexChanged(QString)), this, SLOT( refreshFileConfig() ) );
    connect(ui->comboBox_frameIncOrDec, SIGNAL(currentIndexChanged(QString)), this, SLOT( refreshFileConfig() ) );
    connect(ui->lineEdit_firstline, SIGNAL(textChanged(QString)), this, SLOT( refreshFileConfig() ));
    connect(ui->lineEdit_startOffset, SIGNAL(textChanged(QString)), this, SLOT( refreshFileConfig() ));

}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    //选择文件打开
    QString file_full, file_name, file_path, file_suffix ;


    file_full = QFileDialog::getOpenFileName(this, tr("open file"), " ",  tr("allfile(*.*);;datafile(*.dat)"));

    incDataFileParam_inst.fileinfo = QFileInfo(file_full);
    //文件名
    file_name = incDataFileParam_inst.fileinfo.fileName();
    //文件后缀
    file_suffix = incDataFileParam_inst.fileinfo.suffix();
    //绝对路径
    file_path = incDataFileParam_inst.fileinfo.absolutePath();

//    qDebug() << file_name;
//    qDebug() <<file_suffix;
//    qDebug() <<file_path;

    //
   // incDataCheck(&incDataFileParam_inst);
    refreshFileConfig();
    //disbla all input controls

    //call data check function
    incDataCheck();
}

void MainWindow::incDataCheck()
{
    byte *data_temp;
    data_temp =  (byte *) malloc(sizeof(byte)* data_temp_size);
    UInt64 *data_8bto32b;
    data_8bto32b = (UInt64 *) malloc(sizeof(UInt64)*data_32b_size);
    QString data_file_name = incDataFileParam_inst.fileinfo.fileName();
    QString log_file_name = incDataFileParam_inst.fileinfo.fileName();
    log_file_name.replace(incDataFileParam_inst.fileinfo.suffix(), "txt");


    //QFile data_log_file("binary.file");
    qDebug() <<log_file_name;

    QString dataFilePath = incDataFileParam_inst.fileinfo.absolutePath()+"/"+incDataFileParam_inst.fileinfo.fileName();
    QString logFilePath = incDataFileParam_inst.fileinfo.absolutePath()+"/"+log_file_name;
    qDebug() <<dataFilePath;
    qDebug() <<logFilePath;

    //file path: QString to char*
//    QFile rawDataFile(dataFilePath);
//    QFile logFile(logFilePath);

//    //open files
//    rawDataFile.open(QIODevice::ReadOnly);

//    QByteArray data_temp_8bit ;

//    data_temp_8bit = rawDataFile.read(data_temp_size);

    QByteArray ba = dataFilePath.toUtf8(); // must
    char* file_path_c = ba.data();
    QByteArray ba1 = logFilePath.toUtf8();
    char* file_log_path_c = ba1.data();

    //file IO based on C lib
    FILE *fid;
    FILE *fid_log;
    errno_t err_fid;
    errno_t err_fid_log;
    err_fid = fopen_s(&fid,file_path_c,"rb");
    err_fid_log = fopen_s(&fid_log,file_log_path_c,"w");

    int nRealBytesGet =0;
    //本地造数变量,针对顺序数，只需要一个

    UInt64 localData;
    //错误行数变量
    UInt64 errLineNum;
    //记录最后一行
    UInt64 lastLineNum;


    //open file err handle, if data file open failed
    if(err_fid == 0)
        printLog("data file was opened\n");
    else
    {	printLog("data file open failed\n");
        printLog(QString::number(err_fid));
        return ;
    }

    //open file err handle, if log file open failed
    if(err_fid_log == 0)
        printLog("log file was created\n");
    else
    {	printLog("log file create failed\n");
        return ;
    }


    //起始偏移
    _fseeki64(fid, incDataFileParam_inst.start_offset, SEEK_SET);
    //加入循环
    while(feof(fid) == 0)
    {
        //获取数据
            nRealBytesGet = fread_s(data_temp, data_temp_size*sizeof(char), sizeof(char), data_temp_size, fid);

            //转换数据
            for(int i=0;i<data_32b_size;i++)
            {
                data_8bto32b[i] = data_temp[2 * i] * pow_1 + data_temp[2 * i + 1] * pow_2; // +data_temp[4 * i + 2] * pow_3 + data_temp[4 * i + 3] * pow_4;

            }
    }

}

void MainWindow::refreshFileConfig()
{

    //get file preset parameters from the input controls

    incDataFileParam_inst.lineLength = ui->comboBox_lineLen->currentText().toInt();
    //qDebug() <<lineLength;
    incDataFileParam_inst.frameLength = ui->comboBox_frameLen->currentText().toInt();
    if(ui->comboBox_frameIncOrDec->currentIndex()==0)
        incDataFileParam_inst.frameInc = true;
    else
        incDataFileParam_inst.frameInc = false;

    if(ui->comboBox_dataPowIncOrDec->currentIndex()==0)
        incDataFileParam_inst.powInc = true;
    else
        incDataFileParam_inst.powInc = false;
    //qDebug() <<ui->comboBox_frameIncOrDec->currentText();
    bool ok;
    incDataFileParam_inst.startValue = ui->lineEdit_firstline->text().toULongLong(&ok, 16);
    incDataFileParam_inst.start_offset = ui->lineEdit_startOffset->text().toULongLong();
    qDebug() <<incDataFileParam_inst.frameInc;
    qDebug() <<incDataFileParam_inst.start_offset;



}

//it has some problem, not used
void MainWindow::QString2char(QString  str, char*  ch)
{


    QByteArray ba = str.toUtf8(); // must
    ch=ba.data();


}

//show log at the PlainText control
void MainWindow::printLog(QString str)
{
    ui->plainTextEdit->appendPlainText(str);
}
